#include "BinaryList.h"
#include "Rope.h"
#include "Ropes.h"

/*
template <class E>
BinaryList<E>::BinaryList() : ArrayList() {}
/*
template <class E>
BinaryList<E>::BinaryList(const int& newCapacity) : ArrayList<E>(newCapacity) {}
*/
namespace{
	int min(const int& a, const int& b){
		return a < b ? a : b;
	}

	bool operator<(const Rope& left, const Rope& right)
	{
		int smallestLen = min(Ropes::len(left.getValue()), Ropes::len(right.getValue()));

		for (int i = 0; i < smallestLen; ++i)
		{
			if (left.getValue()[i] < right.getValue()[i])
				return true;
			else if (left.getValue()[i] > right.getValue()[i])
				return false;
			else
				continue;
		}

		if (smallestLen == Ropes::len(left.getValue()))
			return true;

		return false;
	}
	bool operator<(const Pub& left, const Pub& right)
	{
		return left.getName() < right.getName();
	}

	bool operator>(const Rope& left, const Rope& right){
		return right < left;
	}

	bool operator>(const Pub& left, const Pub& right){
		return right < left;
	}
}
template <class E>
BinaryList<E>::~BinaryList() { }


template <class E>
ostream& operator<< (ostream& s, BinaryList<E>& list){
	if (list.size == 0){
		s << "[]";
		return s;
	}

	s << '[' << list[0];
	for (int i = 1; i < list.size; ++i)
		s << ',' << list[i];
	s << ']';
	return s;
}


template <class E>
void BinaryList<E>::add(const E& e){

	int index = find(e, 0, size - 1);
	if (index <= 0) ArrayList<E>::add(e, -index);
	
}



template <class E>
int BinaryList<E>::find(const E& e, const int& min, const int& max){
	
	int mid = (max + min) / 2;

	if (max < min)
		return -min;
	
	if (arrayPointer[mid] > e)
		return find(e, min, mid - 1);
	else 
		if (arrayPointer[mid] < e)
			return find(e, mid + 1, max); 
		else
			return mid;
}


